import java.sql.Connection;
import java.util.Scanner;

public class CommandInterface {
	private DatabaseControl sql=new DatabaseControl();
	private String menu;
	public void CommandInterface() {
	}
	public String getLoginType() {
		System.out.println("1 普通用户登录; 2 普通用户注册; 3 图书管理员登录");
		Scanner sc = new Scanner(System.in);
		String type = sc.nextLine();
		while(true) {
			if(type.equals("1")||type.equals("2")||type.equals("3")) {
				break;
			}
			System.out.println("请输入数字1或2或3");
			type = sc.nextLine();
		}
		return type;
	}
	
	public void getLoginByType(String type) {
		switch(type) {
		case "1":
			this.userLogin();
			break;
		case "2":
			this.userRegister();
			break;
		case "3":
			this.managerLogin();
			break;
		}		
	}
	
	public void managerLogin() {
		Scanner sc = new Scanner(System.in);
		
		System.out.println("请输入管理员用户名");
		String name=sc.nextLine();
		
		System.out.println("请输入管理员密码");
		String pwd=sc.nextLine();
		
		boolean check=sql.checkManager(name,pwd);
		String type;
		if(check) {
			//连接数据库验证
			Manager m=new Manager(sql);
			System.out.println("按照以下提示进行操作");
			System.out.println("out 退出; add 添加新书; delete 移除失效书籍; set 设置库存;");
			String cmd;
	
			while(true) {
				cmd=sc.nextLine();
				if(cmd.equals("out")) {
					break;
				}
				
				switch(cmd) {
				case "add":
					m.addNewBook();
					System.out.println("out 退出; add 添加新书; delete 移除书籍; set 设置库存;");
					break;
				case "delete":
					m.deleteBookByCode();
					System.out.println("out 退出; add 添加新书; delete 移除书籍; set 设置库存;");
					break;
				case "set":
					m.setBookCount();
					System.out.println("out 退出; add 添加新书; delete 移除书籍; set 设置库存;");
					break;
				}
			}
			System.out.println("管理员已退出");
		}
		type=this.getLoginType();
		this.getLoginByType(type);
	}
	
	
	public void userLogin() {
		Scanner sc = new Scanner(System.in);
		
		System.out.println("请输入用户名");
		String name=sc.nextLine();
		
		System.out.println("请输入密码");
		String pwd=sc.nextLine();
		//数据库操作	
		String cmd;
		String type;
		boolean check=sql.checkUser(name,pwd);
		if(check) {
			User u=new User(sql,name);
			System.out.println("按照以下提示进行操作");
			System.out.println("out 退出; return 还书; borrow 借书");
			while(true) {
				cmd=sc.nextLine();
				if(cmd.equals("out")) {
					break;
				}
				
				switch(cmd) {
				case "return":
					u.returnBook();
					System.out.println("out 退出; return 还书; borrow 借书");
					break;
					
				case "borrow":
					u.borrowBook();
					System.out.println("out 退出; return 还书; borrow 借书");
					break;
				}
			}
			System.out.println("退出用户");
		}

		type=this.getLoginType();
		this.getLoginByType(type);
	}
	
	public void userRegister() {
		Scanner sc = new Scanner(System.in);
		String type;
		System.out.println("请输入用户名");
		String name=sc.nextLine();
		
		System.out.println("请输入密码");
		String pwd=sc.nextLine();
		
		System.out.println("确认密码");
		String	checked=sc.nextLine();
		
		if(!pwd.equals(checked)) {
			System.out.println("两次输入的密码不相符！");
		}else if(pwd.length()<6||pwd.length()>50) {
			System.out.println("密码位数不合法，请输入大于6小于50	的密码!");
		}else if(name.indexOf("黄赌毒")!=-1) {
			System.out.println("用户名含有敏感词！");
		}else if(sql.checkUserNameIsExist(name)){
			System.out.println("该用户名已被使用");	
		}else {
			//数据库操作
			//将新注册的用户保存到数据库后
			sql.addNewUser(name, pwd);
			System.out.println("新用户注册成功");			
		}

		type=this.getLoginType();
		this.getLoginByType(type);
	}
}
